package com.example.huanhang.changzhouapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.example.huanhang.changzhouapp.R;
import com.example.huanhang.changzhouapp.base.BaseBackActivity;
import com.example.huanhang.changzhouapp.bean.User;
import com.example.huanhang.changzhouapp.util.DateUtils;
import com.example.huanhang.changzhouapp.util.NumberUtil;
import com.example.huanhang.changzhouapp.util.ThreadUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import jp.wasabeef.richeditor.RichEditor;

/**
 * @author lei
 * @desc 创建公告页面
 * @date 2018/9/5 0005 -- 下午 2:52.
 * 个人博客站: http://www.bestlei.top
 */
public class CreatNoticeActivity extends BaseBackActivity {

    @BindView(R.id.tv_title)
    AppCompatTextView mTvTitle;
    @BindView(R.id.bt_submit)
    AppCompatTextView mBtSubmit;
    @BindView(R.id.title)
    RelativeLayout mTitle;
    @BindView(R.id.et_work_title)
    AppCompatEditText mEtWorkTitle;
    @BindView(R.id.action_undo)
    ImageButton mActionUndo;
    @BindView(R.id.action_redo)
    ImageButton mActionRedo;
    @BindView(R.id.action_bold)
    ImageButton mActionBold;
    @BindView(R.id.action_italic)
    ImageButton mActionItalic;
    @BindView(R.id.action_subscript)
    ImageButton mActionSubscript;
    @BindView(R.id.action_superscript)
    ImageButton mActionSuperscript;
    @BindView(R.id.action_strikethrough)
    ImageButton mActionStrikethrough;
    @BindView(R.id.action_underline)
    ImageButton mActionUnderline;
    @BindView(R.id.action_heading1)
    ImageButton mActionHeading1;
    @BindView(R.id.action_heading2)
    ImageButton mActionHeading2;
    @BindView(R.id.action_heading3)
    ImageButton mActionHeading3;
    @BindView(R.id.action_heading4)
    ImageButton mActionHeading4;
    @BindView(R.id.action_heading5)
    ImageButton mActionHeading5;
    @BindView(R.id.action_heading6)
    ImageButton mActionHeading6;
    @BindView(R.id.action_indent)
    ImageButton mActionIndent;
    @BindView(R.id.action_outdent)
    ImageButton mActionOutdent;
    @BindView(R.id.action_align_left)
    ImageButton mActionAlignLeft;
    @BindView(R.id.action_align_center)
    ImageButton mActionAlignCenter;
    @BindView(R.id.action_align_right)
    ImageButton mActionAlignRight;
    @BindView(R.id.action_insert_bullets)
    ImageButton mActionInsertBullets;
    @BindView(R.id.action_insert_numbers)
    ImageButton mActionInsertNumbers;
    @BindView(R.id.action_blockquote)
    ImageButton mActionBlockquote;
    @BindView(R.id.action_insert_link)
    ImageButton mActionInsertLink;
    @BindView(R.id.editor)
    RichEditor mEditor;
    private AlertDialog linkInputDialog;
    private String mUserName;

    @Override
    public int getLayoutId() {
        return R.layout.activity_creat_notice;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void initData() {
        mTvTitle.setText("公告发布");
    }


    @OnClick({R.id.bt_submit, R.id.action_undo, R.id.action_redo, R.id.action_bold, R.id.action_italic, R.id.action_subscript, R.id.action_superscript, R.id.action_strikethrough, R.id.action_underline, R.id.action_heading1, R.id.action_heading2, R.id.action_heading3, R.id.action_heading4, R.id.action_heading5, R.id.action_heading6, R.id.action_indent, R.id.action_outdent, R.id.action_align_left, R.id.action_align_center, R.id.action_align_right, R.id.action_insert_bullets, R.id.action_insert_numbers, R.id.action_blockquote, R.id.action_insert_link})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_submit:
                submit();
                break;
            case R.id.action_undo:
                mEditor.undo();
                break;
            case R.id.action_redo:
                mEditor.redo();
                break;
            case R.id.action_bold:
                mEditor.setBold();
                break;
            case R.id.action_italic:
                mEditor.setItalic();
                break;
            case R.id.action_subscript:
                mEditor.setSubscript();
                break;
            case R.id.action_superscript:
                mEditor.setSuperscript();
                break;
            case R.id.action_strikethrough:
                mEditor.setStrikeThrough();
                break;
            case R.id.action_underline:
                mEditor.setUnderline();
                break;
            case R.id.action_heading1:
                mEditor.setHeading(1);
                break;
            case R.id.action_heading2:
                mEditor.setHeading(2);
                break;
            case R.id.action_heading3:
                mEditor.setHeading(3);
                break;
            case R.id.action_heading4:
                mEditor.setHeading(4);
                break;
            case R.id.action_heading5:
                mEditor.setHeading(5);
                break;
            case R.id.action_heading6:
                mEditor.setHeading(6);
                break;
            case R.id.action_indent:
                mEditor.setIndent();
                break;
            case R.id.action_outdent:
                mEditor.setOutdent();
                break;
            case R.id.action_align_left:
                mEditor.setAlignLeft();
                break;
            case R.id.action_align_center:
                mEditor.setAlignCenter();
                break;
            case R.id.action_align_right:
                mEditor.setAlignRight();
                break;
            case R.id.action_insert_bullets:
                mEditor.setBullets();
                break;
            case R.id.action_insert_numbers:
                mEditor.setNumbers();
                break;
            case R.id.action_blockquote:
                mEditor.setBlockquote();
                break;
            case R.id.action_insert_link:
                onLinkInsert();
                break;
        }
    }

    private void submit() {
        String title = mEtWorkTitle.getText().toString();
        String mEditorHtml = mEditor.getHtml();
        if (TextUtils.isEmpty(title) || TextUtils.isEmpty(mEditorHtml)) {
            toast("请填写完整");
            return;
        }
        insertNotice(title, mEditorHtml);
    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventMainThread(User user) {
        mUserName = user.getName();

    }
    private void insertNotice(String title, String html) {
        ThreadUtil.runOnBackThread(() -> {
            String sql = "INSERT INTO gonggao(id,lccb_name,lccb_time,lccb_title,lccb_content) VALUES('"+ NumberUtil.getRandomString(32)+"','"+mUserName+"','"+ DateUtils.getCurrentDateHms()+"','"+title+"','"+html+"')";

            try {
                boolean executeInsert = ThreadUtil.executeInsert(sql);
                if (!executeInsert) {
                    Message message = createHandler.obtainMessage(0);
                    createHandler.sendMessage(message);
                } else {
                    Message message = createHandler.obtainMessage(1);
                    createHandler.sendMessage(message);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    /**
     * 处理返回的结果
     */
    private Handler createHandler = new Handler(msg -> {
        int what = msg.what;
        switch (what) {
            case 1:
                dismissWaitDialog();
                toast("提交失败");
                break;
            case 0:
                dismissWaitDialog();
                toast("提交成功");
                activityFinish();
                break;
            default:
                break;
        }
        return false;
    });

    private void onLinkInsert() {
        if (linkInputDialog == null) {
            View linkInputView = LayoutInflater.from(this).inflate(R.layout.dialog_link, null);
            final EditText etText = linkInputView.findViewById(R.id.et_text);
            final EditText etLink = linkInputView.findViewById(R.id.et_link);
            linkInputDialog = new AlertDialog.Builder(this)
                    .setTitle("添加链接")
                    .setView(linkInputView)
                    .setCancelable(true)
                    .setPositiveButton("确定", (dialog, which) -> {
                        String text = etText.getText().toString().trim();
                        String link = etLink.getText().toString().trim();
                        if (text.isEmpty() || link.isEmpty()) {
                            toast("内容不能为空");
                            return;
                        }
                        mEditor.insertLink(link, text);
                    }).create();
        }
        linkInputDialog.show();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
